import React from 'react';

import {WeaUpload,WeaBrowser,WeaHelpfulTip,WeaDialog,WeaLocaleProvider,WeaTools, WeaSearchGroup, WeaFormItem,WeaInput,WeaNewScroll,WeaSelectGroup,WeaTable,WeaInputNumber,WeaColorPicker,WeaSelect,WeaCheckbox} from 'ecCom'
const getLabel = WeaLocaleProvider.getLabel;
import {WeaPortalShare,WeaMaterialLib} from 'weaPortalPublic';
import './style/';
import {Tabs, Button, Spin, Icon, message, Modal,Row,Col} from 'antd';
import Add from "./add";
const TabPane = Tabs.TabPane;
class Setting extends React.Component{

    state={

        addVisiable: false ,
        setId:-1,
        selectedRowKeys:"",

    }

    setConfig = (config)=> {
        let data = this.props.data
        let setting = Object.assign(data.setting, config)
        data.setting = setting
        this.props.setStateData({"data":data});
    }



    setStateData = (data)=>{
        this.setState(data)
    }

    onCancle = ()=>{
        this.props.onLoad();
        this.props.setStateData({
            settingVisiable:false
        })
    }

    onOk = ()=>{
        const {id,setting} = this.props.data
        WeaTools.callApi('/api/portal/customCompanyNews/updateCompanyNews', 'POST', {"json":JSON.stringify({id:id,setting:JSON.stringify(setting)})}).then((result) => {
            if (result.status == true) {
                this.onCancle()
                message.success(getLabel(83551, '保存成功！'), 3);
            } else {
                message.error(getLabel(84544, '保存失败！'), 3);
            }
        });
    }


    delete = (ids)=>{

        Modal.confirm({
            title: getLabel(15172, '系统提示'),
            content: getLabel(385625, '确定要删除选择的记录吗？'),
            onOk: () => {
                WeaTools.callApi('/api/portal/customCompanyNews/deleteCompanyNewsItem', 'POST', {"json":JSON.stringify(ids)}).then((result) => {
                    if (result.status == true) {
                       this.props.onLoad()
                        message.success(getLabel(83551, '保存成功！'), 3);
                    } else {
                        message.error(getLabel(84544, '保存失败！'), 3);
                    }
                });
            },
        });

    }

    deleteAll = (ids)=>{

        Modal.confirm({
            title: getLabel(15172, '系统提示'),
            content: getLabel(385625, '确定要删除选择的记录吗？'),
            onOk: () => {
                WeaTools.callApi('/api/portal/customCompanyNews/deleteCompanyNewsItem', 'POST', {"json":JSON.stringify(ids)}).then((result) => {
                    if (result.status == true) {
                        this.setState({selectedRowKeys:[]})
                        this.props.onLoad()
                        message.success(getLabel(83551, '保存成功！'), 3);
                    } else {
                        message.error(getLabel(84544, '保存失败！'), 3);
                    }
                });
            },
        });

    }

    getCols = ()=>{
        let cols = [{
            title: getLabel(195, '名称'),
            dataIndex: 'title',
            key: 'title',
            width: "50%",
        },{
            title: getLabel(30585, '操作'),
            key: 'operation',
            width: "50%",
            render: (text, record) => (
                <span>
                    <input type="hidden" name="id" value={record.id}/>
                   <div>
                        <span className="esetting-dso-button" onClick={()=>{this.setState({'addVisiable':true,'setId':record.id})}}>{getLabel(30747, '设置')}</span>
                        <span className="esetting-dso-button" onClick={()=>{let ids = [];ids.push(record.id);this.delete(ids)}}>{getLabel(91, '删除')}</span>


                   </div>
                </span>
            )
        }]
        return cols;
    }

    getData = ()=>{
        let items = this.props.data.items
        let realData = []
        for(let i in items){
            realData.push({
                key:items[i].id,
                id: items[i].id,
                title: items[i].setting.title,
            })


        }
        return realData
    }

    onSelectChange = (selectedRowKeys)=>{

        this.setState({
            selectedRowKeys:selectedRowKeys
        })
    }

    drop = (datas)=>{
        let query = []
        for(let i in datas) {
            query.push({"id": datas[i].id, "orderIndex": parseInt(i)+1})
        }

        if(query.length >1){
            WeaTools.callApi('/api/portal/customCompanyNews/orderCompanyNewsItemCmd', 'POST', {"json":JSON.stringify(query)}).then((result) => {
            });
        }

    }



    render (){
        const {addVisiable} = this.state
        const {showNumber} = this.props.data.setting
        const {visiable} = this.props

        const rowSelection = {
            selectedRowKeys : this.state.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: (record)=>{
                return {
                    disabled: (record.createBySystem == 1 && this.props.container.isSystem==0)    // 配置无法勾选的列
                };
            }
        };
        return (
            <WeaDialog
                title={getLabel(30747, '设置')}
                visible={visiable}
                iconBgcolor='#1a57a0'
                hasScroll={false}
                style={{width: 900,height : 500}}
                onCancel={()=>{this.onCancle()}}
            >
                <Spin spinning={false}>
                    <Tabs defaultActiveKey="1" className="portal-esetting-tabs" onChange={()=>{}}>
                        <TabPane tab={getLabel(33368, '内容')} key="1">
                            <WeaSearchGroup title={getLabel(81711, '基本信息')} needTigger={true} showGroup={true}>
                                <div style={{'margin':'20px 50px'}}>
                                    <div style={{'margin-top':'10px'}}>
                                        <Row>
                                            <Col span={6} >
                                                <div>{getLabel(19493, '显示条数')} :</div>
                                            </Col>
                                            <Col span={18}>
                                                <WeaInputNumber  value={showNumber} viewAttr={2} onChange={value => this.setConfig({showNumber: value})}/>
                                            </Col>
                                        </Row>
                                    </div>

                                </div>
                            </WeaSearchGroup>
                            <WeaSearchGroup title={getLabel(20885, '内容来源')} needTigger={true} showGroup={true} customComponent={
                                <div>
                                    <Button type="primary" className="portal-esetting-wsg-btn" onClick={()=>{this.setState({'addVisiable':true,'setId':-1})}} >
                                        {getLabel(611, '添加')}
                                    </Button>
                                    {
                                        this.state.selectedRowKeys.length >0
                                            ?  (
                                                    <Button type={"primary"} className="lr_managerSetting_center_delete_button"  onClick={() => {this.deleteAll(this.state.selectedRowKeys)}} >{getLabel(32136, '批量删除')}</Button>
                                                )
                                            :   (
                                                    <Button type={"ghost"} className="lr_managerSetting_center_delete_button"  >{getLabel(32136, '批量删除')}</Button>
                                                 )
                                    }
                                </div>


                            }>
                                <WeaNewScroll
                                    height={300}
                                    onScroll={(e)=>{

                                    }}>
                                    <WeaTable rowSelection={rowSelection}  columns={this.getCols()} dataSource={this.getData()}  pagination={false} draggable={true} onDrop={this.drop}/>

                                </WeaNewScroll>
                            </WeaSearchGroup>
                        </TabPane>

                    </Tabs>
                </Spin>
                <div className="portal-esetting-confirm lr_managerSetting_option_buttons_div" >
                    <Button type="primary" onClick={() => {this.onOk()}}>{getLabel(30986, '保存')}</Button>
                    <Button type="ghost"  style={{marginLeft: '10px'}} onClick={()=>{this.onCancle()}}>{getLabel(31129, '取消')}</Button>
                </div>

                <Add data={this.props.data} visiable={this.state.addVisiable} setId={this.state.setId} setStateData={this.setStateData} onLoad={this.props.onLoad}></Add>

            </WeaDialog>
            )

    }

}
export default Setting